package com.example.ka;



import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class dangban extends AppCompatActivity {
    EditText etCount;
    EditText etid;
    Button ok_btn;
    private Mysql helper;
    SQLiteDatabase db;
    public dangban() {
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dangban);
        TextView tv = findViewById(R.id.tv);
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
        Date date = new Date(System.currentTimeMillis());
        //获取当前时间

        String datestr = simpleDateFormat.format(date);

        tv.setText(datestr);
        helper = new Mysql(dangban.this);
        etid = findViewById(R.id.etid);
        etCount = findViewById(R.id.etCount);
        ok_btn=findViewById(R.id.btnA);

        ok_btn.setOnClickListener(v -> {
            String id = etid.getText().toString().trim();
            String data = tv.getText().toString().trim();
            String Count = etCount.getText().toString().trim();
            if ("".equals(Count)) {
//            如果价格为空 就会有一个弹窗
                Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            } else {
                db = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("id", id);
                values.put("beginData",data);
                values.put("Money", Count);
                long account = db.insert("account", null, values);
                if (account > 0) {
                    Toast toast = Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    setResult(1);
                    Intent intent = new Intent(dangban.this,shouyin.class);
                    startActivity(intent);
                } else {
                    Toast toast = Toast.makeText(this, "请重试", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    db.close();
                }
                setResult(1);
            }
        });

        }

}